package com.seari.utils;

import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.CollectionUtils;

import com.seari.bean.DeviceList;
import com.seari.bean.DeviceType;
import com.seari.bean.DeviceTypeAttr;

public class FEPDeviceListHelper
{
	public Workbook generateWorkBook(Map<String,DeviceType> DeviceTypeMap,List<DeviceList> exportList ,String line)
    {
		Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet();
        generateHead(sheet);
        int rowNum = 1;
        rowNum = generateBody(rowNum,sheet, exportList, line);
        return wb;
    }
	
	//生成表头
	public void generateHead(Sheet sheet)
	{
		Row row = sheet.createRow(0);
		row.createCell(0).setCellValue("点来源"); //点来源
		row.createCell(1).setCellValue("完整名称"); //完整名称
		row.createCell(2).setCellValue("通道类型"); //通道类型
		row.createCell(3).setCellValue("描述"); //描述
		row.createCell(4).setCellValue("类型"); //类型
		row.createCell(5).setCellValue("标记"); //标记
		row.createCell(6).setCellValue("权限"); //权限
		row.createCell(7).setCellValue("采集周期(ms)"); //采集周期(ms)
		row.createCell(8).setCellValue("IO.n[0]"); //IO.n[0]
		row.createCell(9).setCellValue("IO.n[1]"); //IO.n[1]
		row.createCell(10).setCellValue("IO.n[2]"); //IO.n[2]
		row.createCell(11).setCellValue("IO.n[3]"); //IO.n[3]
		row.createCell(12).setCellValue("IO.n[4]"); //IO.n[4]
		row.createCell(13).setCellValue("IO.n[5]"); //IO.n[5]
		row.createCell(14).setCellValue("IO.n[6]"); //IO.n[6]
		row.createCell(15).setCellValue("IO.n[7]"); //IO.n[7]
		row.createCell(16).setCellValue("IO.n[8]"); //IO.n[8]
		row.createCell(17).setCellValue("IO.n[9]"); //IO.n[9]
		row.createCell(18).setCellValue("IO.n[10]"); //IO.n[10]
		row.createCell(19).setCellValue("IO.n[11]"); //IO.n[11]
		row.createCell(20).setCellValue("IO.csData[0]"); //IO.csData[0]
		row.createCell(21).setCellValue("IO.csData[1]"); //IO.csData[1]
		row.createCell(22).setCellValue("IO.csData[2]"); //IO.csData[2]
		row.createCell(23).setCellValue("IO.csData[3]"); //IO.csData[3]
		row.createCell(24).setCellValue("IO.csData[4]"); //IO.csData[4]
		row.createCell(25).setCellValue("IO.csData[5]"); //IO.csData[5]
		row.createCell(26).setCellValue("opc_vtDataType"); //opc_vtDataType
		row.createCell(27).setCellValue("opc_csItemID"); //opc_csItemID
		row.createCell(28).setCellValue("opc_csAccessPath"); //opc_csAccessPath
		row.createCell(29).setCellValue("IoLinker.m_nKind"); //IoLinker.m_nKind
		row.createCell(30).setCellValue("IoLinker.m_nMode"); //IoLinker.m_nMode
		row.createCell(31).setCellValue("IoLinker.m_csObject"); //IoLinker.m_csObject
		row.createCell(32).setCellValue("IoLinker.m_csPath"); //IoLinker.m_csPath
		row.createCell(33).setCellValue("模拟量额定值"); //模拟量额定值
		row.createCell(34).setCellValue("模拟量初始值"); //模拟量初始值
		row.createCell(35).setCellValue("模拟量取绝对值"); //模拟量取绝对值
		row.createCell(36).setCellValue("模拟量CT/PT"); //模拟量CT/PT
		row.createCell(37).setCellValue("模拟量系数"); //模拟量系数
		row.createCell(38).setCellValue("模拟量倍率系数"); //模拟量倍率系数
		row.createCell(39).setCellValue("模拟量基数"); //模拟量基数
		row.createCell(40).setCellValue("模拟量基础倍率"); //模拟量基础倍率
		row.createCell(41).setCellValue("模拟量量程变换"); //模拟量量程变换
		row.createCell(42).setCellValue("模拟量裸数据上限"); //模拟量裸数据上限
		row.createCell(43).setCellValue("模拟量裸数据下限"); //模拟量裸数据下限
		row.createCell(44).setCellValue("模拟量量程上限"); //模拟量量程上限
		row.createCell(45).setCellValue("模拟量量程下限"); //模拟量量程下限
		row.createCell(46).setCellValue("数字量初始值"); //数字量初始值
		row.createCell(47).setCellValue("数字量采集取反"); //数字量采集取反
		row.createCell(48).setCellValue("数字量真值描述"); //数字量真值描述
		row.createCell(49).setCellValue("数字量假值描述"); //数字量假值描述
	}
	
	public int generateBody(int rowNum, Sheet sheet, List<DeviceList> exportList ,String line)
	{
		for (DeviceList deviceList : exportList)
		{
			if(null != deviceList.getDeviceType() && !CollectionUtils.isEmpty(deviceList.getDeviceType().getAttrList()))
			{
				for (DeviceTypeAttr deviceTypeAttr : deviceList.getDeviceType().getAttrList())
                {
                    if("N".equals(deviceTypeAttr.getIscsInfo().toUpperCase()) || "1".equals(deviceTypeAttr.getIscsInfo()))
                    {
                        continue;
                    }
                    else 
                    {
                    	Row row = sheet.createRow(rowNum);
                    	String deviceId = deviceList.getDevIndex().split("_")[1];
                    	row.createCell(0).setCellValue(3); //点来源
                		row.createCell(1).setCellValue(deviceList.getStationID() + "FAS.FAS." + deviceId + "_" + deviceTypeAttr.getIndex()); //完整名称
                		row.createCell(2).setCellValue(0); //通道类型
                		row.createCell(3); //描述
                		if(deviceList.getDriverType().equals(DeviceListHelper.SYM_DRIVER) && line.toUpperCase().equals("M") && deviceList.getSysID().equals("FASX"))
                		{
	                		row.createCell(4).setCellValue(2); //类型
	                		row.createCell(5).setCellValue(0); //标记
	                		row.createCell(6).setCellValue(0); //权限
	                		row.createCell(7).setCellValue(1000); //采集周期(ms)
	                		row.createCell(8).setCellValue(1); //IO.n[0]
	                		try
							{
	                			row.createCell(9).setCellValue(Integer.parseInt(deviceList.getBeizhu2()) + Integer.parseInt(deviceTypeAttr.getIscsAddr()) + Integer.parseInt(deviceTypeAttr.getIscsBit())); //IO.n[1]
							} catch (Exception e)
							{
								// do nothing
								continue;
							}
	                		row.createCell(10).setCellValue(-1); //IO.n[2]
	                		row.createCell(11).setCellValue(0); //IO.n[3]
	                		row.createCell(12).setCellValue(0); //IO.n[4]
	                		row.createCell(13).setCellValue(0); //IO.n[5]
	                		row.createCell(14).setCellValue(0); //IO.n[6]
	                		row.createCell(15).setCellValue(0); //IO.n[7]
	                		row.createCell(16).setCellValue(0); //IO.n[8]
	                		row.createCell(17).setCellValue(0); //IO.n[9]
	                		row.createCell(18).setCellValue(0); //IO.n[10]
	                		row.createCell(19).setCellValue(0); //IO.n[11]
	                		row.createCell(20); //IO.csData[0]
	                		row.createCell(21); //IO.csData[1]
	                		row.createCell(22); //IO.csData[2]
	                		row.createCell(23); //IO.csData[3]
	                		row.createCell(24); //IO.csData[4]
	                		row.createCell(25); //IO.csData[5]
	                		row.createCell(26).setCellValue(0); //opc_vtDataType
	                		row.createCell(27); //opc_csItemID
	                		row.createCell(28); //opc_csAccessPath
	                		row.createCell(29).setCellValue(0); //IoLinker.m_nKind
	                		row.createCell(30).setCellValue(0); //IoLinker.m_nMode
	                		row.createCell(31); //IoLinker.m_csObject
	                		row.createCell(32); //IoLinker.m_csPath
	                		row.createCell(33); //模拟量额定值
	                		row.createCell(34); //模拟量初始值
	                		row.createCell(35); //模拟量取绝对值
	                		row.createCell(36); //模拟量CT/PT
	                		row.createCell(37); //模拟量系数
	                		row.createCell(38); //模拟量倍率系数
	                		row.createCell(39); //模拟量基数
	                		row.createCell(40); //模拟量基础倍率
	                		row.createCell(41); //模拟量量程变换
	                		row.createCell(42); //模拟量裸数据上限
	                		row.createCell(43); //模拟量裸数据下限
	                		row.createCell(44); //模拟量量程上限
	                		row.createCell(45); //模拟量量程下限
	                		row.createCell(46).setCellValue(0); //数字量初始值
	                		row.createCell(47).setCellValue(0); //数字量采集取反
	                		row.createCell(48).setCellValue("合"); //数字量真值描述
	                		row.createCell(49).setCellValue("分"); //数字量假值描述
                		}else if(deviceList.getDriverType().equals(DeviceListHelper.SYM_DRIVER) && line.toUpperCase().equals("M") && deviceList.getSysID().equals("AFCX"))
                		{
                			if(deviceTypeAttr.getIoType().toUpperCase().equals(DeviceListHelper.AI_IOTYPE))
                			{
                				//模拟量
                				row.createCell(4).setCellValue(1); //类型
                				row.createCell(5).setCellValue(0); //标记
    	                		row.createCell(6).setCellValue(0); //权限
    	                		row.createCell(7).setCellValue(1000); //采集周期(ms)
    	                		row.createCell(8).setCellValue(2); //IO.n[0]
    	                		try
    							{
    	                			row.createCell(9).setCellValue(Integer.parseInt(deviceList.getBeizhu2())); //IO.n[1]
    							} catch (Exception e)
    							{
    								// do nothing
    								continue;
    							}
    	                		row.createCell(10).setCellValue(1); //IO.n[2] 数据类型，0是位，1是无符号整型
    	                		row.createCell(11).setCellValue(0); //IO.n[3]
    	                		row.createCell(12).setCellValue(-1); //IO.n[4]
    	                		row.createCell(13).setCellValue(0); //IO.n[5]
    	                		row.createCell(14).setCellValue(0); //IO.n[6]
    	                		row.createCell(15).setCellValue(0); //IO.n[7]
    	                		row.createCell(16).setCellValue(0); //IO.n[8]
    	                		row.createCell(17).setCellValue(0); //IO.n[9]
    	                		row.createCell(18).setCellValue(0); //IO.n[10]
    	                		row.createCell(19).setCellValue(0); //IO.n[11]
    	                		row.createCell(20); //IO.csData[0]
    	                		row.createCell(21); //IO.csData[1]
    	                		row.createCell(22); //IO.csData[2]
    	                		row.createCell(23); //IO.csData[3]
    	                		row.createCell(24); //IO.csData[4]
    	                		row.createCell(25); //IO.csData[5]
    	                		row.createCell(26).setCellValue(0); //opc_vtDataType
    	                		row.createCell(27); //opc_csItemID
    	                		row.createCell(28); //opc_csAccessPath
    	                		row.createCell(29).setCellValue(0); //IoLinker.m_nKind
    	                		row.createCell(30).setCellValue(0); //IoLinker.m_nMode
    	                		row.createCell(31); //IoLinker.m_csObject
    	                		row.createCell(32); //IoLinker.m_csPath
    	                		row.createCell(33).setCellValue(0); //模拟量额定值
    	                		row.createCell(34).setCellValue(0); //模拟量初始值
    	                		row.createCell(35).setCellValue(0); //模拟量取绝对值
    	                		row.createCell(36).setCellValue(0); //模拟量CT/PT
    	                		row.createCell(37).setCellValue(0); //模拟量系数
    	                		row.createCell(38).setCellValue(0); //模拟量倍率系数
    	                		row.createCell(39).setCellValue(0); //模拟量基数
    	                		row.createCell(40).setCellValue(1); //模拟量基础倍率
    	                		row.createCell(41).setCellValue(0); //模拟量量程变换
    	                		row.createCell(42).setCellValue(0); //模拟量裸数据上限
    	                		row.createCell(43).setCellValue(0); //模拟量裸数据下限
    	                		row.createCell(44).setCellValue(0); //模拟量量程上限
    	                		row.createCell(45).setCellValue(0); //模拟量量程下限
    	                		row.createCell(46); //数字量初始值
    	                		row.createCell(47); //数字量采集取反
    	                		row.createCell(48); //数字量真值描述
    	                		row.createCell(49); //数字量假值描述
                			}else if(deviceTypeAttr.getIoType().toUpperCase().equals(DeviceListHelper.DI_IOTYPE))
                			{
                				//数字量
                				row.createCell(4).setCellValue(2); //类型
                				row.createCell(5).setCellValue(0); //标记
    	                		row.createCell(6).setCellValue(0); //权限
    	                		row.createCell(7).setCellValue(1000); //采集周期(ms)
    	                		row.createCell(8).setCellValue(2); //IO.n[0] 
    	                		try
    							{
    	                			row.createCell(9).setCellValue(Integer.parseInt(deviceList.getBeizhu2())); //IO.n[1]
    							} catch (Exception e)
    							{
    								// do nothing
    								continue;
    							}
    	                		row.createCell(10).setCellValue(0); //IO.n[2] 数据类型，0是位，1是无符号整型
    	                		row.createCell(11).setCellValue(Integer.parseInt(deviceTypeAttr.getIscsAddr())); //IO.n[3]
    	                		row.createCell(12).setCellValue(-1); //IO.n[4]
    	                		row.createCell(13).setCellValue(0); //IO.n[5]
    	                		row.createCell(14).setCellValue(0); //IO.n[6]
    	                		row.createCell(15).setCellValue(0); //IO.n[7]
    	                		row.createCell(16).setCellValue(0); //IO.n[8]
    	                		row.createCell(17).setCellValue(0); //IO.n[9]
    	                		row.createCell(18).setCellValue(0); //IO.n[10]
    	                		row.createCell(19).setCellValue(0); //IO.n[11]
    	                		row.createCell(20); //IO.csData[0]
    	                		row.createCell(21); //IO.csData[1]
    	                		row.createCell(22); //IO.csData[2]
    	                		row.createCell(23); //IO.csData[3]
    	                		row.createCell(24); //IO.csData[4]
    	                		row.createCell(25); //IO.csData[5]
    	                		row.createCell(26).setCellValue(0); //opc_vtDataType
    	                		row.createCell(27); //opc_csItemID
    	                		row.createCell(28); //opc_csAccessPath
    	                		row.createCell(29).setCellValue(0); //IoLinker.m_nKind
    	                		row.createCell(30).setCellValue(0); //IoLinker.m_nMode
    	                		row.createCell(31); //IoLinker.m_csObject
    	                		row.createCell(32); //IoLinker.m_csPath
    	                		row.createCell(33); //模拟量额定值
    	                		row.createCell(34); //模拟量初始值
    	                		row.createCell(35); //模拟量取绝对值
    	                		row.createCell(36); //模拟量CT/PT
    	                		row.createCell(37); //模拟量系数
    	                		row.createCell(38); //模拟量倍率系数
    	                		row.createCell(39); //模拟量基数
    	                		row.createCell(40); //模拟量基础倍率
    	                		row.createCell(41); //模拟量量程变换
    	                		row.createCell(42); //模拟量裸数据上限
    	                		row.createCell(43); //模拟量裸数据下限
    	                		row.createCell(44); //模拟量量程上限
    	                		row.createCell(45); //模拟量量程下限
    	                		row.createCell(46).setCellValue(0); //数字量初始值
    	                		row.createCell(47).setCellValue(0); //数字量采集取反
    	                		row.createCell(48).setCellValue("合"); //数字量真值描述
    	                		row.createCell(49).setCellValue("分"); //数字量假值描述
                			}
                		}
                		rowNum++;
                    }
                }
			}
		}
		return rowNum;
	}
}
